package dao;

import java.io.Serializable;
import java.util.List;

import modelo.Barrio;
import modelo.Localidad;
import modelo.Provincia;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import util.HibernateUtil;

public class BarrioDao implements Serializable{

	private static final long serialVersionUID = -9175754992555232126L;

	private Session session;
	SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
	
	public BarrioDao() {
		super();
	}
	
	public List<Barrio> obtenerBarrios() {
		session=sessionFactory.openSession();
		
		Query query = session.createQuery("SELECT b FROM Barrio b");
		List<Barrio> b = query.list();
		
		session.close();
		
		return b;
	}

	public void guardarBarrio(Barrio barrio) {
		session = sessionFactory.openSession();
		session.beginTransaction();
		session.saveOrUpdate(barrio);
		session.getTransaction().commit();
		session.close();
	}
	
	public List<Barrio> obtenerBarrios(Localidad l) {
		session = sessionFactory.openSession();

		Query query = session.createQuery("from Barrio where idLocalidad = :id ");
		query.setParameter("id", l.getIdLocalidad());

		List<Barrio> list = query.list();
		
		return list;
	}
}
